package com.sinodata.bsm.cicp.finder.weblogic8;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sinodata.bsm.cicp.finder.Finder;

public class WebAppFinder extends Finder {

    private static final Logger logger = Logger.getLogger(WebAppFinder.class);

    /*@Override
    @SuppressWarnings("unchecked")
    public List find(ResValue pResValue, ResAttributeValValue[] pAttrVals, ResAttributeValue[] attributes) {
        String ip = pResValue.getIp();
        String port = pAttrVals[0].getAttrValue();
        String username = pAttrVals[1].getAttrValue();
        String password = pAttrVals[2].getAttrValue();
        String serverName = pAttrVals[3].getAttrValue();

        List resources = new ArrayList();

        String xmlString = XmlGetter.getJmxXml("WlgWebAppInfo", ip, port, username, password);
        // parse xml
        try {
            Document document = DocumentHelper.parseText(xmlString);
            Element rootElement = document.getRootElement();
            Iterator<?> iterator = rootElement.elementIterator("WebAppInfo");
            while (iterator.hasNext()) {
                Element element = (Element) iterator.next();
                if (element != null) {
                    String svrName = element.element("ServerName").getTextTrim();
                    if (!svrName.equals(serverName))
                        continue;
                    Element nameElement = element.element("Name");
                    String name = nameElement.getTextTrim();
                    Object oneResource = CreateOneResource(serverName, name, attributes);
                    resources.add(oneResource);
                }
            }
        } catch (Exception e) {
            logger.error("failed to find weblogic8 webapp", e);
        }
        return resources;
    }*/

    /*@SuppressWarnings("unchecked")
    private Object CreateOneResource(String serverName, String name, ResAttributeValue[] attributes) {
        List oneResource = new ArrayList();
        // Resource
        ResValue res = new ResValue();
        res.setIp(host); // host IP address
        res.setName(name);// resource name
        res.setInternalId(host + "_weblogic8_" + serverName + "_" + name); // Internal
        // ID
        // for
        // agent
        res.setDescript("WebApplication:" + name); // resource description
        oneResource.add(res);

        // Resource attributes
        ResAttributeValValue attribute = new ResAttributeValValue();
        attribute.setResAttributeId(attributes[0].getId());
        attribute.setAttrValue(name);
        oneResource.add(attribute);

        return oneResource;
    }*/

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        String port = (String) params.get("PORT");
        String username = (String) params.get("USERNAME");
        String password = (String) params.get("PASSWORD");
        String serverName = (String) params.get("SERVERNAME");
        List<String[]> list = new ArrayList<String[]>();

        String xmlString = XmlGetter.getJmxXml("WlgWebAppInfo", ip, port, username, password);
        // parse xml
        try {
            Document document = DocumentHelper.parseText(xmlString);
            Element rootElement = document.getRootElement();
            Iterator<?> iterator = rootElement.elementIterator("WebAppInfo");
            while (iterator.hasNext()) {
                Element element = (Element) iterator.next();
                if (element != null) {
                    String svrName = element.element("ServerName").getTextTrim();
                    if (!svrName.equals(serverName))
                        continue;
                    Element nameElement = element.element("Name");
                    String name = nameElement.getTextTrim();
                    String[] info = new String[4];
                    list.add(info);
                    info[0] = ip + "_weblogic8_" + serverName + "_" + name;
                    info[1] = name;
                    info[2] = "WebApplication:" + name;
                    info[3] = name;
                }
            }
            return list;
        } catch (Exception e) {
            logger.error("failed to find weblogic8 webapp", e);
            return new ArrayList<String[]>();
        }
    }

}
